package com.ys.controller.web.business;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ys.config.MenuId;
import com.ys.entity.business.TbPlatformServicePayAccount;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/business/tbPlatformServicePayAccount")
public class WebTbPlatformServicePayAccountController {

	@Autowired
	private CommonApiService<TbPlatformServicePayAccount> commonApiService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){

		try {
			TbPlatformServicePayAccount entity = new TbPlatformServicePayAccount();
			ThrOperateCompanyApply thrOperateCompanyApply =  new ThrOperateCompanyApply().setCuser(entity.getOperatorUserId()).queryFirst();
			Map<String, Object> extraParam = new HashMap<String, Object>();

			extraParam.put("platFormCode", "Amazon");
			extraParam.put("serviceCode", "PTFW001");
			extraParam.put("serviceName", "透明计划");
			extraParam.put("operatorUserName", thrOperateCompanyApply.getCUserName());
			entity.setBid(commonApiService.getCommonNextUkey(MenuId.TB_PlatformServicePayAccount, "Bid"))
					.setStatus(1);
			return commonApiService.toAdd(model,entity, MenuId.TB_PlatformServicePayAccount, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		if (params.get("id") == null){
			return W.error(model, "id不能为空");
		}

		Long id = Long.valueOf((params.get("id").toString()));

		if (new TbPlatformServicePayAccount().setId(id).count() == 0){
			return W.error(model, "该信息不存在!");
		}

		try {
			Map<String,Object> entity = new TbPlatformServicePayAccount()
					.addLeftJoin("TP_AmazonShopExchangeRate C on C.Id=A.AmazonShopExchangeRateld")
					.addLeftJoin("THR_OperateCompanyApply B on B.VGsBm=A.Vgsbm")
					.addField("C.Symbol AS amazonShopExchangeRate")
					.addField("B.VGsBm AS vgsbm")
					.addField("B.VGsJc AS vgsjc")
					.setId(id).queryFirstMap();
			Map<String, Object> extraParam = new HashMap<>();
			//关联数据
			extraParam.put("amazonShopExchangeRate", entity.get("amazonShopExchangeRate"));
			extraParam.put("amazonShopExchangeRateld", entity.get("amazonShopExchangeRateld"));
			extraParam.put("vgsbm", entity.get("vgsbm"));
			extraParam.put("vgsjc", entity.get("vgsjc"));
			model.addAttribute("onProcess", 0);
			return commonApiService.toEdit(model, id, new TbPlatformServicePayAccount(), MenuId.TB_PlatformServicePayAccount, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据出错，请联系客服!");
		}
	}

}
